<?php

namespace JLPay\Models\Openmerch\Bill;

use JLPay\Models\BaseResponse;

/**
 * 结算账单查询响应
 * 
 * @author liaochangyi
 * @since 2025-01-25
 */
class SettleBillQueryResponse extends BaseResponse
{
    protected ?string $errMsg = null;  // 错误信息
    protected ?string $merchNo = null;  // 商户号
    protected ?string $startBusiDate = null;  // 开始营业日期，yyyy-MM-dd
    protected ?string $endBusiDate = null;  // 结束营业日期
    protected ?string $startSettleDate = null;  // 开始结算日期，yyyy-MM-dd
    protected ?string $endSettleDate = null;  // 结束结算日期
    protected ?string $payAmt = null;  // 付款金额
    protected ?string $transCount = null;  // 交易笔数
    protected ?array $billDetailList = null;  // 账单明细列表

    /**
     * 构造函数
     *
     * @param array $data 响应数据
     */
    public function __construct(array $data)
    {
        parent::__construct($data);
        
        $this->errMsg = $data['err_msg'] ?? null;
        $this->merchNo = $data['merch_no'] ?? null;
        $this->startBusiDate = $data['start_busi_date'] ?? null;
        $this->endBusiDate = $data['end_busi_date'] ?? null;
        $this->startSettleDate = $data['start_settle_date'] ?? null;
        $this->endSettleDate = $data['end_settle_date'] ?? null;
        $this->payAmt = $data['pay_amt'] ?? null;
        $this->transCount = $data['trans_count'] ?? null;
        
        // 处理账单明细列表
        $billDetailListData = $data['bill_detail_list'] ?? [];
        if ($billDetailListData && is_array($billDetailListData)) {
            $this->billDetailList = [];
            foreach ($billDetailListData as $detail) {
                $this->billDetailList[] = new BillDetail($detail);
            }
        }
    }

    /**
     * 获取错误信息
     *
     * @return string|null
     */
    public function getErrMsg(): ?string
    {
        return $this->errMsg;
    }

    /**
     * 获取商户号
     *
     * @return string|null
     */
    public function getMerchNo(): ?string
    {
        return $this->merchNo;
    }

    /**
     * 获取付款金额
     *
     * @return string|null
     */
    public function getPayAmt(): ?string
    {
        return $this->payAmt;
    }

    /**
     * 获取交易笔数
     *
     * @return string|null
     */
    public function getTransCount(): ?string
    {
        return $this->transCount;
    }

    /**
     * 获取账单明细列表
     *
     * @return BillDetail[]|null
     */
    public function getBillDetailList(): ?array
    {
        return $this->billDetailList;
    }

    /**
     * 转换为数组
     *
     * @return array
     */
    public function toArray(): array
    {
        $data = parent::toArray();
        $data['err_msg'] = $this->errMsg;
        $data['merch_no'] = $this->merchNo;
        $data['start_busi_date'] = $this->startBusiDate;
        $data['end_busi_date'] = $this->endBusiDate;
        $data['start_settle_date'] = $this->startSettleDate;
        $data['end_settle_date'] = $this->endSettleDate;
        $data['pay_amt'] = $this->payAmt;
        $data['trans_count'] = $this->transCount;
        
        // 转换账单明细列表
        if ($this->billDetailList) {
            $data['bill_detail_list'] = [];
            foreach ($this->billDetailList as $detail) {
                $data['bill_detail_list'][] = $detail->toArray();
            }
        } else {
            $data['bill_detail_list'] = null;
        }
        
        return $data;
    }
} 